<?php

	if(isset($_POST['install'])){
		include("../config.php");
		$myFile = "../config.php";
		$fh = fopen($myFile, 'w');
		$contents = "<?php\n" .
			"define('DB_HOST', '" . $_POST['host'] . "');\n" .
			"define('DB_USER', '" . $_POST['username'] ."');\n" .
			"define('DB_PASS', '" . $_POST['password'] ."');\n" .
			"define('DB_CHAR', '" . $_POST['char'] ."');\n" .
			"define('DB_NAME', '" . $_POST['name'] ."');\n" .
			"define('APP_TZ', '" . $_POST['tz'] . "');\n" .
			"define('APP_INSTALL', true);\n" .
			"define('APP_VERSION', '" . APP_VERSION . "');\n" .
			"?>";
		fwrite($fh, $contents);
		fclose($fh);
		//include("../common.php");
		define('DB_HOST', $_POST['host']);
		define('DB_USER', $_POST['username']);
		define('DB_PASS', $_POST['password']);
		define('DB_CHAR', $_POST['char']);
		define('DB_NAME', $_POST['name']);
		mysql_connect(DB_HOST, DB_USER, DB_PASS);
		mysql_query("SET NAMES " . DB_CHAR);
		mysql_query("CREATE DATABASE `" . $_POST['name'] . "`");
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<title>197遊學園 - 安裝程式</title>
	</head>
	<body>
		<h1>步驟二</h1>
		<h3>安裝完成</h3>
		<p>恭喜您！安裝程式已完成，請將db.sql匯入至資料庫。請按<a href="../index.php">這裡</a>看看您的網站。若成功運行，請務必將install資料夾刪除。記住，您隨時都可以運行install/index.php。</p>
	</body>
</html>
<?php
	} else {
	
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<title>197遊學園 - 安裝程式</title>
	</head>
	<body>
		<h1>步驟一</h1>
		<h3>輸入安裝設定</h3>
		<p>請在下列欄位裡輸入您的伺服器設定。</p>
		<p><strong>注意：在繼續進行前，請先將根目錄中的config.php設為可讀寫（777）。</strong></p>
		<form method="POST" action="index.php">
			<input type="hidden" name="install" value="true" />
			<p><label>資料庫主機：<input type="text" name="host" value="localhost" /></label></p>
			<p><label>資料庫帳號：<input type="text" name="username" /></label></p>
			<p><label>資料庫密碼：<input type="password" name="password" /></label></p>
			<p><label>資料庫名稱：<input type="text" name="name" value="197website" /></label></p>
			<p><label>資料庫編碼：<input type="text" name="char" value="UTF8" /></label></p>
			<p><label>伺服器時區：<input type="text" name="tz" value="Asia/Taipei" /></label></p>
			<p><input type="submit" value="安裝" /></p>
		</form>
	</body>
</html>
<?php } ?>